<%@ page language="java" contentType="text/html;charset=UTF-8" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack"%>

<chiptrack:checkLogon sourcePage="/processTypeSetup.do"/>
<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
               com.scalar.chiptrack.users.Functionality,
               com.scalar.chiptrack.users.User,
               java.util.ArrayList"%>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel=stylesheet>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onUnload="closeWindow()">

<SCRIPT language="JavaScript">
<%	ArrayList unitsList = ( ArrayList )session.getAttribute( "units" );

	if( unitsList != null )
	{
	    int length = unitsList.size();
%>
	    var unitsArray = new Array( '<%=length%>' );

    <%	for( int i = 0; i < length; i++ )
        {
            com.scalar.chiptrack.commons.UOM units = (com.scalar.chiptrack.commons.UOM)unitsList.get( i );
%>
            var uom		 = '<%=units.getUom()%>';
            var baseUOM  = '<%=units.getBaseUom()%>';
            var minValue = '<%=units.getMinValue()%>';
            var maxValue = '<%=units.getMaxValue()%>';
            var units  = new Array( uom, baseUOM, minValue, maxValue );
            unitsArray[ <%=i%> ] = units;
<%
    	}
	}
%>
  function addRow( rowId )
  {
      var row = document.getElementById(rowId);
      var table = row.parentElement;
      var newRow = table.insertRow(row.rowIndex);
      var td = newRow.insertCell();
      td.width = "62";
      td.innerHTML="&nbsp;";
      td = newRow.insertCell();
      td.width="128";
      td.innerHTML = '<input type=text name="processParameterVal" class=txfield size="20" maxlength="50" />';
      td = newRow.insertCell();
  }

  function save()
  {
      var errors	= "";
    var li      = document.getElementById( "message" );
    li.style.visibility = "visible";
    var paramUnit = '<%=request.getParameter ( "paramUnit" )%>';
    var paramName  = document.processTypeSetupForm.processParameter.value;
      if( typeof document.processTypeSetupForm.processParameterVal != 'undefined' )
      {
          if( typeof document.processTypeSetupForm.processParameterVal.length != 'undefined' )
          {
              var noOfParamValues = document.processTypeSetupForm.processParameterVal.length;
              //check for validity..
              for ( var i=0; i < noOfParamValues; i++ )
              {
                var paramValue = document.processTypeSetupForm.processParameterVal[i].value;
                checkParameterValues( paramUnit, paramValue, paramName );
                if ( errMsg.length != 0 )
                {
                    li.innerHTML = errMsg;
                    document.processTypeSetupForm.processParameterVal[i].focus();
                    return ;
                }
              }
              //searching for the duplicates..
              var unitArr = getUnitArr ( paramUnit );
              if ( unitArr == null )
              {
                return;
              }
              var baseUom = unitArr[1];
              for( var k = 0; k < noOfParamValues; k++ )
              {
                  var name  = document.processTypeSetupForm.processParameterVal[k].value;
                  document.processTypeSetupForm.processParameterVal[k].value = name;

                 if( name != '' )
                  {
                      for( var i = 0; i < k; i++ )
                      {
                          var lastName = document.processTypeSetupForm.processParameterVal[i].value;
                          lastName = lastName;

                          if ( baseUom == 'STRING' )
                          {
                              if( name.trim().toUpperCase() == lastName.trim().toUpperCase() )
                              {
                                  errors = errors+"<li> <%=request.getParameter ( "processParameter" )%> already exists please select another name";
                                  li.innerHTML = errors;
                                  document.processTypeSetupForm.processParameterVal[k].focus();
                                  return ;
                              }
                          }
                          else if ( baseUom == 'INT' )
                          {
                             if( parseInt( name.trim() ) == parseInt( lastName.trim() ) )
                             {
                                errors = errors+"<li> <%=request.getParameter ( "processParameter" )%> already exists please select another value";
                                li.innerHTML = errors;
                                document.processTypeSetupForm.processParameterVal[k].focus();
                                return ;
                             }
                          }
                          else if ( baseUom ==  'DOUBLE' )
                          {
                            if ( Number( name.trim() ) == Number( lastName.trim() ) )
                            {
                                errors = errors+"<li> <%=request.getParameter ( "processParameter" )%> already exists please select another value";
                                li.innerHTML = errors;
                                document.processTypeSetupForm.processParameterVal[k].focus();
                                return ;
                            }
                          }
                      }
                  }
              }
          }
          else
          {
              if( document.processTypeSetupForm.processParameterVal.value.trim() == '')
              {
                   errors = errors+"<li> Please enter the <%=request.getParameter ( "processParameter" )%>";
                   li.innerHTML = errors;
                  return;
              }
              else
              {
                var paramValue = document.processTypeSetupForm.processParameterVal.value;
                checkParameterValues( paramUnit, paramValue, paramName );
                if ( errMsg.length != 0 )
                {
                    li.innerHTML = errMsg;
                    document.processTypeSetupForm.processParameterVal.focus();
                    return ;
                }
              }
          }
      }

      if( typeof document.processTypeSetupForm.processParameterVal != 'undefined' )
      {
          if( typeof document.processTypeSetupForm.processParameterVal.length != 'undefined' )
          {
              var noOfParamValues = document.processTypeSetupForm.processParameterVal.length;
              for( var k = 0; k < noOfParamValues; k++ )
              {
                  document.processTypeSetupForm.processParameterVal[k].disabled = false;
              }
          }
          else
          {
              document.processTypeSetupForm.processParameterVal.disabled = false;
          }
      }

      document.processTypeSetupForm.action='<%=chipTracContext%>/processTypeSetup.do?action=saveProcessParameterValues' ;
      document.processTypeSetupForm.submit();
  }

  var closeFlag = false;
  function closeWindow()
  {
      if ( !closeFlag )
      {
          closeFlag = true;
          opener.parent.refresh();
      }
      window.close();
  }

  var errMsg = "";
  function getUnitArr ( unit )
  {
    for( var i = 0; i < unitsArray.length; i++ )
    {
        unitArr = unitsArray[ i ];

        if( unitArr[0] == unit )
        {
            break;
        }
        unitArr = null;
    }
    return unitArr;
  }
    function checkParameterValues( unit, value, name )
    {
        var unitArr;
        var baseUom;
        var minValue;
        var maxValue;
        errMsg = "";
        /*for( var i = 0; i < unitsArray.length; i++ )
        {
            unitArr = unitsArray[ i ];

            if( unitArr[0] == unit )
            {
                break;
            }
            unitArr = null;
        }*/
        unitArr = getUnitArr ( unit );
        if( unitArr == null )
        {
            return;
        }

        var baseUom = unitArr[1];
        var minValue = unitArr[2];
        var maxValue = unitArr[3];

        if( baseUom == 'DOUBLE' )
        {
            checkFloat( value, name + ' value ', 'N' );
            if( errMsg.length == 0 )
            {
                if( Number( value ) < Number( minValue ) )
                {
                    errMsg = "<li>" + name + " value must be greater than " + minValue;
                }
                else if( Number( value ) > Number( maxValue ) )
                {
                    errMsg = "<li>" + name + " Value must be less than or equal to "+ maxValue;
                }
            }
            else
            {
                errMsg = "<li>" + errMsg;
            }
        }
        else if ( baseUom == 'INT' )
        {
            checkInteger( value, name + ' value ', 'N' );
            if( errMsg.length == 0 )
            {
                if( parseInt( value ) < parseInt( minValue ) )
                {
                    errMsg = "<li>" + name + " value must be greater than " + minValue;
                }
                else if( parseInt( value ) > parseInt( maxValue ) )
                {
                    errMsg = "<li>" + name + " Value must be less than or equal to  "+ maxValue;
                }
            }
            else
            {
                errMsg = "<li>" + errMsg;
            }
        }
        else if ( baseUom == 'STRING' )
        {
            if( errMsg.length == 0 )
            {
                if( value.length < minValue  )
                {
                    errMsg = "<li>" + name + " value must be of length greater " + minValue;
                }
                else if( value.length > maxValue )
                {
                    errMsg = "<li>" + name + " value must be of length less than "+ maxValue;
                }
            }
            else
            {
                errMsg = "<li>" + errMsg;
            }
        }
    }
</SCRIPT>

<html:form name="processTypeSetupForm"  type="com.scalar.chiptrack.operations.processtypesetup.forms.ProcessTypeSetupForm" action="/processTypeSetup.do">

<TABLE cellSpacing=0 cellPadding=0 width="100%" border="0">
  <TBODY>
  <TR>
    <TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'></TD>
      <TD bgcolor="#ffffff">
      <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
      <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
      <TD valign="top" width="100%">
      <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
      <BR>
      <!-- from here your body starts -->
      <TABLE border="0">
          <TR>
          <TD>&nbsp;</TD>
          <TD colspan =2>
                  <font class='NArialS'><font color='orange'><b>
                    <li>To add new <%=request.getParameter ( "processParameter" )%>, click on [+]
                    </b>
                </font>
              </TD>
          </TR>
          <tr>
              <td colspan=3><font class="NArialL"><font align=left color="FF0000" id=message ></font></td>
          </tr>
          <TR><TD colspan =3 class="NArialL"><chiptrack:messages type="info" styleClass="NArialL" /></TD></TR>
          <tr><td colspan=3><chiptrack:info styleClass="NArialL"/></td></tr>
        <tr><td colspan=3><chiptrack:errors styleClass="NArialL"/></td></tr>
          <TR>
              <TD colspan="3" bgColor="#993333" width="263" height="16"><FONT class="NArialL" style="color:#ffffff" id="fieldLabel">&nbsp;Add New <%=request.getParameter ( "processParameter" )%>:</FONT></TD>
          </TR>
          <%
              int colno = 0;
              ArrayList paramValuesList = (ArrayList)request.getAttribute("processParameterValues");
          %>
          <% for( int i=0; i < paramValuesList.size(); i++ ){ %>
          <TR>
              <TD width="62">&nbsp;</TD>
              <TD width="128">
                  <input type=text name="processParameterVal"  value = "<%=paramValuesList.get(i)%>"class=txfield size="20" maxlength="50" disabled = "true" />
              </TD>
              <TD><% if ( colno == 0 ) { %> <a href="javascript:addRow('rowId1');"><img border=0 src='<%=chipTracContext%>/jsp/includes/images/add.jpg' height='15' width='20' align=center onmouseover="window.status='Click here to add row';return true" title="Click here to add row" onmouseout="window.status='';return true" ></a> <% } %></TD>
             <% colno++;%>
          </TR>
          <%}if( paramValuesList.size() == 0 ){%>
           <TR>
              <TD width="62">&nbsp;</TD>
              <TD width="128">
                  <input type=text name="processParameterVal" class=txfield size="20" maxlength="50" />
              </TD>
              <TD><a href="javascript:addRow('rowId1');"><img border=0 src='<%=chipTracContext%>/jsp/includes/images/add.jpg' height='15' width='20' align=center onmouseover="window.status='Click here to add row';return true" title="Click here to add row" onmouseout="window.status='';return true" ></a></TD>
          </TR>
          <%}%>
          <TR  id="rowId1"><TD colspan="3" ></TD></TR>
          <TR>
              <TD width="62">&nbsp;</TD>
              <TD colspan="2" width="195">&nbsp;</TD>
          </TR>
          <TR>
              <TD width="62">&nbsp;</TD>
              <TD align="center">
                  <a href="javascript:save();"><IMG onmouseover="window.status='Click here to Save';return true" title="Click here to Save" onmouseout="window.status='';return true" height=17 src="<%=chipTracContext%>/jsp/includes/images/save.gif"  width=53 border=0></a>
                  <a href="javascript:document.processTypeSetupForm.reset();"><IMG onmouseover="window.status='Click to Reset';return true" title="Click here to Reset" onmouseout="window.status='';return true" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" border=0 width="53" height="17"></a>
              </TD>
              <TD colspan="5">&nbsp;</TD>
          </TR>
          <TR>
              <TD colspan="6" align="center">
                  <a href="javascript:closeWindow();"  onMouseOver="window.status='Click here to close this window';return true" title='Click here to close this window'><font class='NArialL'><b>[Close this window]</b></font></a>
              </TD>
          </TR>
      </TABLE>
  <input type="hidden" name="processType" value="<%=request.getParameter ( "processType" )%>">
  <input type="hidden" name="processParameter" value="<%=request.getParameter ( "processParameter" )%>">

</html:form>
  </TD>
<!-- till here -->
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>



